<?php
/**
 * 问题控制器
 * @author 王涛
 * @DataTime 2018-08-02T08:46:15+0800
 */
namespace app\api\controller;
use app\api\model\LikenumModel;
use app\api\model\CarmationModel;
use think\Request;

class Carmation extends Base{
    /**
     * 问题列表
     * @return mixed
     * @throws \think\exception\DbException
     * @author zhenHong~
     * 2018-7-19 接口重写
     */
    public function selectMation(){
        $Carmation = new CarmationModel();
        $limit = input('listNum') ?: 10;
        $pageNum = input('pagenum') ?: 1;
        $where = array_filter([
            'status' => ['neq',9],
            'title' => input('searchText') ? ['like','%'.input('searchText').'%'] : ''
        ],function($data){
            if($data){
                return true;
            }else{
                return false;
            }
        });
        $selectResult = $Carmation->getMationList($where, $pageNum, $limit);
        return apiResponse(200,"查询成功",$selectResult);
    }

    /**
     * 问题详情
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @author zhenHong~
     * 2018-7-19 接口重写
     */
    public function mationInfo(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarmationValidate.mationInfo');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $Carmation = new CarmationModel();
        $data = $Carmation->mationInfo($postData['id']);
        return apiResponse(200,"查询成功",$data);
    }
    /**
     * 添加常见问题
     * @author topqiang~ 
     * @DataTime 2018.7.20
     * @return  成功返回id 失败返回报错信息
     */
    public function addMation(Request $request){
        $postData = $request->post();
        $result = $this->validate($postData,'CarmationValidate.addMation');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $car = new CarmationModel();
        $id = $car ->addMation($postData);
        if($id){
            return apiResponse(200,"添加常见问题成功",["id"=>$id]);
        }else{
            return apiResponse(201,"添加常见问题失败","");
        }
    }
    /**
     * 添加图片
     * @author 王涛
     * @DataTime 2018-08-02T10:34:27+0800
     * @return   [type]                   成功返回图片路径
     * 2018-7-20 添加注释 zhenHong~
     */
    public function addPic(Request $request)
    {
        $postData = $request->post();
        $result = $this->validate($postData,'CarmationValidate.addPic');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        //将图片名称打散为数组
        $temp = explode('.', $postData['pic_name']);
        //设置图片的唯一名称 uniqid()函数会产生一个随机的13位字符串
        $ext = uniqid() . '.' . end($temp);
        //获取图片base64
        $base64 = substr(strstr($postData['pic'], ","), 1);
        //将base64转化为图片
        $image_res = base64_decode($base64);
        //设置图片存放路径
        $pic_link = "upload/mation/" . date('Y-m-d') . '/' . $ext;
        //设置图片文件存放目录
        $saveRoot = "upload/mation/" . date('Y-m-d') . '/';
        //检查目录是否存在  循环创建目录
        if (!is_dir($saveRoot)) {
            mkdir($saveRoot, 0777, true);
        }
        //将图片存放到对应的文件目录中
        $res = file_put_contents($pic_link, $image_res);
        if ($res) {
            $data['imgPath'] = '/' . $pic_link;
            return apiResponse(200, "上传成功", $data);
        } else {
            return apiResponse(201, "上传失败", "");
        }
    }
    /**
     * 修改问题
     * @author zhenhong~ 接口重写
     * @dateTime 2018-08-20
     */
    public function editMation(Request $request){
        $postData = $request->post();
        $result = $this->validate($postData,'CarmationValidate.editMation');
        if($result !== true){
            return apiResponse(201,$result,'');
        }
        $cash = new CarmationModel();
        $res = $cash ->editMation($postData);
        if($res){
            return apiResponse(200,"问题编辑成功","");
        }else{
            return apiResponse(201,"问题编辑失败","");
        }
    }
    /**
     * 删除问题
     * @author 王涛
     * @DataTime 2018-08-02T13:53:24+0800
     * @return   [type]                   [description]
     * 2018-7-24    zhenHong~修改
     */
    public function deletMation(Request $request){
        $post = $request->post();
//        $result = $this->validate($post,'CarmationValidate.deletMation');
//        if($request !== true){
//            return apiResponse(201,$result,'');
//        }
        if(empty($post["id"])){
            return apiResponse(201,"问题id 不能为空","");
        }
        $cash = new CarmationModel();
        $num = $cash->deleteMation($post['id']);
        if($num){
            return apiResponse(200,"问题删除成功","");
        }else{
            return apiResponse(201,"问题删除失败","");
        }
    }

}